#pragma once

#include "Utility\XmlParser.h"
#include "Utility\FixedArray.h"
#include "Utility\LinkedList.h"
#include "Utility\Map.h"
#include "Utility\Matrix.h"
#include "Animation.h"

class ColladaGeometry;

class ColladaFile
{
public:
	ColladaFile(void);
	~ColladaFile(void);

	typedef struct _sController
	{
		u32							uID;
		u32							uGeometryID;
		int							iVertexCount;
		FixedArray<u32>*			paBones;
		FixedArray<Matrix>			aInverseBinds;
		FixedArray<u32>				aBoneIndices;
		FixedArray<Vector4>			aVertexWeights;

	}sController;

	typedef struct _sNodeData
	{
		u32 uName;
		char* pData;
	}sNodeData;

	typedef struct _sBoneData
	{
		int		iIndex;
		u32		uParent;	
		u32		uName;
		char*	pFloats;
		Matrix	mWorld;
	}sBoneData;
	

	bool								Load(const char* cszFileName);
	ColladaGeometry*					Geometry(int iIndex)			{ return m_aGeometries[iIndex]; }
	int									GeometryCount()					{ return m_aGeometries.GetNumElements(); }
	Map<u32, ColladaFile::sBoneData>*	GetBoneIndiceMap()				{ return m_pBoneIndiceMap; }
	FixedArray<Animation::sBone*>*		GetBones()						{ return m_paBones; }
	FixedArray<Matrix>*					GetInverseBinds()				{ return m_paInvBinds; }

protected:
	void LoadGeometryLibrary(XmlParser* pParser);
	ColladaGeometry* LoadMesh(XmlParser* pParser);
	
	void LoadVertices(XmlParser* pParser, LinkedList& sources);
	u16* LoadTriangles(XmlParser* pParser, LinkedList& sources, int& iOutIndexCount);
	
	void LoadLibraryControllers(XmlParser *pParser);
	void LoadBoneIndicesWeights(sController* pController, sSource* pWeightSource, XmlParser* pParser, const char* pszIndexCounts);
	void LoadBoneIndices(XmlParser* pParser, LinkedList* pSources, sController* pController);
	void LoadInverseBinds(XmlParser* pParser, LinkedList* pSources);
	void LoadAnimationLibrary(XmlParser* pParser);
	void LoadVisualScene(XmlParser* pParser);
	void LoadNode(XmlParser* pParser);
	void LoadBone(XmlParser* pParser, u32 uParent);
	bool CreateSkeletonMap(u32 uBone);
	void LoadMaterials(XmlParser* pParser);
	void LoadEffects(XmlParser* pParser);
	void LoadImages(XmlParser* pParser);

protected:
	FixedArray<ColladaGeometry*>			m_aGeometries;
	LinkedList*								m_plControllers;	

	FixedArray<Matrix>*						m_paInvBinds;
	FixedArray<Animation::sBone*>*			m_paBones;
	Map<u32, sBoneData>*					m_pBoneIndiceMap;
	LinkedList*								m_plBoneHierarchy;
	LinkedList*								m_plSkeletonSources;
	Map<u32, u32>							m_materialEffectMap;
	Map<u32, u32>							m_effectMaterialMap;
	Map<u32, const char*>					m_materialTextureMap;
	Map<u32, u32>							m_textureMaterialMap;
	
};
